using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Material.DAL
{
    public class Accessor : IAccessor
    {
        internal SqlConnection conn = new SqlConnection();
        public string CommandText
        {
            get { return _cmd.CommandText; }
            set { _cmd.CommandText = value.Trim(); }
        }

        public CommandType CommandType
        {
            get { return _cmd.CommandType; }
            set { _cmd.CommandType = value; }
        }

        private readonly SqlCommand _cmd;
        public SqlCommand Command
        {
            get { return _cmd; }
        }

        private readonly SqlDataAdapter da;
        public SqlDataAdapter DataAdapter
        {
            get { return da; }
        }

        private SqlTransaction trans;
        public SqlTransaction Transaction
        {
            get { return trans; }
        }


        public Accessor(bool transactionIsUsed, string connString)
        {
            conn.ConnectionString = connString;
            if (conn.State != ConnectionState.Open)
                conn.Open();

            if (transactionIsUsed)
            {
                trans = conn.BeginTransaction();
                _cmd = conn.CreateCommand();
                _cmd.Transaction = trans;
            }
            else
            {
                trans = null;
            }

        }

        public Accessor(string connString)
        {
            try
            {

                conn.ConnectionString = connString;
                if (conn.State != ConnectionState.Open)
                    conn.Open();

                _cmd = new SqlCommand();
                _cmd = conn.CreateCommand();
                da = new SqlDataAdapter(_cmd);
            }
            catch (Exception ex)
            {
                WriteMessage(ex.Message);
            }
        }



        public void WriteMessage(string message)
        {
            //disini tulis erronya kenapa dan kemana.
        }


        public void Add(string name, object value)
        {
            SqlParameter param = new SqlParameter();
            param.ParameterName = name;
            param.Value = value;
            _cmd.Parameters.Add(param);
        }

        public void Close()
        {
            _cmd.Parameters.Clear();
            if (trans == null)
            {
                conn.Close();
                conn.Dispose();
            }
        }

        public void Clear()
        {
            _cmd.Parameters.Clear();
        }

        public void CommitTransaction()
        {
            if (trans != null)
            {
                trans.Commit();
                trans = null;
                _cmd.Parameters.Clear();
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
        }

        public void RollBackTransaction()
        {
            if (trans != null)
            {
                trans.Rollback();
                trans = null;
                _cmd.Parameters.Clear();
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
        }
    }
}
